package ldh.msg.ui.pref;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.BoxLayout;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.border.TitledBorder;

import ldh.msg.MsgConfigInfo;
import ldh.msg.MsgManager;
import ldh.msg.ui.helper.MsgUIUtils;
import ldh.msg.ui.helper.MyNetworkPanel;

public class NetworkPanel extends JSplitPane {
	
	private JPanel myNWListPanel = null;
	private JButton addSndBtn = null;
	
	public NetworkPanel() {
		super(JSplitPane.HORIZONTAL_SPLIT);
	}
	public void initialize() throws Exception {
		JPanel topPanel = new JPanel(new BorderLayout());
		topPanel.setBorder(new TitledBorder("My Network"));
		myNWListPanel = new JPanel();
		myNWListPanel.setLayout(new BoxLayout(myNWListPanel, BoxLayout.Y_AXIS));
		
		String[] tempList = MsgConfigInfo.getInstance().getNwList();
		
		if (tempList != null) {
			String[] ipList = null;
			for (String s : tempList) {
				ipList = s.split(":");
				if (ipList == null || ipList.length != 4) {
					break;
				}
				MyNetworkPanel p = new MyNetworkPanel();
				if (ipList[0] != null && ipList[0].equals("T")) {
					p.setUseChkBx(true);
				}
				p.setIpATF(ipList[1]);
				p.setIpBTF(ipList[2]);
				p.setIpCTF(ipList[3]);
				myNWListPanel.add(p);
			}
		}		
		JScrollPane nwListScollPane = new JScrollPane(myNWListPanel);
		nwListScollPane.getVerticalScrollBar().setUnitIncrement(10);
		
		JPanel btnPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
		btnPanel.setPreferredSize(new Dimension(200, 30));
		addSndBtn = MsgUIUtils.makeBtn("Add", 35, 20);
		addSndBtn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				MyNetworkPanel p = new MyNetworkPanel();
				myNWListPanel.add(p);
				myNWListPanel.revalidate();
			}
		});
		btnPanel.add(addSndBtn);
		
		topPanel.add(new JLabel(MsgManager.getInstance().getHostIpAddr(true)), BorderLayout.NORTH);
		topPanel.add(nwListScollPane, BorderLayout.CENTER);
		topPanel.add(btnPanel, BorderLayout.SOUTH);
		
		JPanel bottomPanel  = new JPanel();
		bottomPanel.setBorder(new TitledBorder("Network Group"));
		bottomPanel.setLayout(new GridLayout(6, 1));
		ButtonGroup grpBtnGrp = new ButtonGroup();
		JRadioButton[] grpRadioBtnList = new JRadioButton[6];
		grpRadioBtnList[0] = new JRadioButton();
		grpRadioBtnList[0].setText("A");
		grpBtnGrp.add(grpRadioBtnList[0]);		
		bottomPanel.add(grpRadioBtnList[0]);
		
		grpRadioBtnList[1] = new JRadioButton();
		grpRadioBtnList[1].setText("B");
		grpBtnGrp.add(grpRadioBtnList[1]);		
		bottomPanel.add(grpRadioBtnList[1]);
		
		grpRadioBtnList[2] = new JRadioButton();
		grpRadioBtnList[2].setText("C");
		grpBtnGrp.add(grpRadioBtnList[2]);		
		bottomPanel.add(grpRadioBtnList[2]);
		
		grpRadioBtnList[3] = new JRadioButton();
		grpRadioBtnList[3].setText("D");
		grpBtnGrp.add(grpRadioBtnList[3]);		
		bottomPanel.add(grpRadioBtnList[3]);
		
		grpRadioBtnList[4] = new JRadioButton();
		grpRadioBtnList[4].setText("E");
		grpBtnGrp.add(grpRadioBtnList[4]);		
		bottomPanel.add(grpRadioBtnList[4]);
		
		grpRadioBtnList[5] = new JRadioButton();
		grpRadioBtnList[5].setText("F");
		grpBtnGrp.add(grpRadioBtnList[5]);		
		bottomPanel.add(grpRadioBtnList[5]);
		int index = 0;
		try {
			index = Integer.parseInt(MsgConfigInfo.getInstance().getNwGrp());
		} catch (NumberFormatException nfe) {
			nfe.printStackTrace();
		}
		grpRadioBtnList[index].setSelected(true);
		setDividerSize(2);
		setResizeWeight(0.5);
		setDividerLocation(250);
		setEnabled(false);
		add(topPanel, JSplitPane.LEFT);
		add(bottomPanel, JSplitPane.RIGHT);
	}
	
	public void doApply() {
		try {
      		System.out.println("myNWListPanel : " + myNWListPanel.getComponentCount());
      		
      		int cnt = myNWListPanel.getComponentCount();
      		
      		if (cnt > 0) {
      			MyNetworkPanel p = null;
      			ArrayList<String> ipArr = new ArrayList<String>();
      			for (int i = 0 ; i < cnt ; i++) {
      				p = (MyNetworkPanel)myNWListPanel.getComponent(i);
      				if (p.getIpATF() != null && ! p.getIpATF().equals("")
      						&& p.getIpBTF() != null && ! p.getIpBTF().equals("")
      						&& p.getIpCTF() != null && ! p.getIpCTF().equals("")) {
      					if (p.isUseChkBx()) {
      						ipArr.add("T:" + p.getIpATF() + ":" + p.getIpBTF() + ":" + p.getIpCTF());
      					} else {
      						ipArr.add("F:" + p.getIpATF() + ":" + p.getIpBTF() + ":" + p.getIpCTF());
      					}      					
      				}
      			}      		
      			MsgConfigInfo.getInstance().setNwList(ipArr.toArray(new String[ipArr.size()]));
      		}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public void doOk() {
	}
	
	public void doCancel() {
	}
}
